home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / PortComm / PortComm.h < prev    next >
Encoding:
Text File  |  1992-08-17  |  8.5 KB  |  178 lines

  1. // -------------------------------------------------------------------------------------
  2. // PortComm.h
  3. // serial port communication handler
  4. // Author: Steve Herrick, Martin D. Flynn, NeXT Computer, Inc.
  5. // -------------------------------------------------------------------------------------
  6. // THIS CODE FRAGMENT IS FOR DEMONSTRATION PURPOSES ONLY.
  7. // Permission is granted to freely redistribute this source code, and to use fragments
  8. // of this code in your own applications if you find them to be useful.  This class,
  9. // along with the source code, come with no warranty of any kind, and the user assumes
  10. // all responsibility for its use.
  11. // -------------------------------------------------------------------------------------
  12.  
  13. // -------------------------------------------------------------------------------------
  14. #import <mach/cthreads.h>
  15. #import <sys/ttydev.h>
  16. #import <sys/ioctl.h>
  17. #import <objc/Object.h>
  18.  
  19. // -------------------------------------------------------------------------------------
  20.  
  21. /* port numbers */
  22. #define         portNumA                0
  23. #define         portA                   "/dev/ttya"
  24.  
  25. #define         portNumB                1
  26. #define         portB                   "/dev/ttyb"
  27.  
  28. /* parity */
  29. #define         parityANY               ANYP
  30. #define         parityODD               ODDP
  31. #define         parityEVEN              EVENP
  32. #define         parityNONE              0
  33. #define         parityRAW               RAW                        // 8 bit, no i/o processing
  34. #define         parityRAW_eol            (0x8000 | parityRAW)    // RAW with EOL checking
  35. #define         parityUNKNOWN            parityNONE
  36.  
  37. /* errors */
  38. #define         noERROR                 0
  39. #define         errorOPEN               1
  40. #define         errorTIOCGETP           2
  41. #define         errorTIOCSETP           3
  42. #define         errorBADPORTNUM         4
  43. #define         errorTIOCEXCL           5
  44. #define         errorTIOCSETD            6
  45. #define         errorTIOCCBRK            7
  46. #define         errorTIOCSDTR            8
  47. #define         errorTIOCLSET            9
  48. #define         errorTIOCNOTTY            10
  49. #define         errorTIOCSLTC            11
  50.  
  51. // -------------------------------------------------------------------------------------
  52. @interface PortComm : Object
  53. {
  54.   
  55.   id            dataHandler;    // data record handler
  56.  
  57.   char          *rcdBuff;       // record buffer
  58.   int            maxBytes;        // default max number of bytes to read
  59.   u_char        charMask;        // character mask
  60.   BOOL            checkEol;        // check EOL even if parity if RAW
  61.  
  62.   mutex_t        forkMutex;      // fork/kill mutex
  63.   mutex_t        writeMutex;     // port write mutex
  64.   
  65.   cthread_t     portThread;     // port listener thread number
  66.   BOOL          isListening;    // port listener running flag
  67.  
  68.   char          portNumber;     // port number (0 == A, 1 == B)
  69.   char          *portDevice;    // port device file name
  70.   char          portBaud;       // port baud rate
  71.   short         portParity;     // port parity
  72.   
  73.   int           portFd;         // port file descriptor
  74.   int           portError;      // chad error
  75.   
  76. }
  77.  
  78. // -------------------------------------------------------------------------------------
  79. // New instance
  80. + newPort:(int)portNum baud:(u_char)baudRate parity:(u_short)parity charMask:(u_char)mask;
  81. + newPort:(int)portNum baud:(u_char)baudRate parity:(u_short)parity;
  82. // These methods create a new instance of a serial port handling object.  portNum is
  83. // either 0, or 1, (for serial ports A & B, respectively).  baudRate is the port
  84. // configuration flag to specify the port Baud rate (see 'baudRateConstant:').  parity is
  85. // is any of the following constant values (parityANY, parityODD, parityEVEN, parityNONE,
  86. // parityRAW, or parityRAW_eol).  charMask is a mask with which each character read from
  87. // the port is AND'ed with.  Typical masks are 0xFF, or 0x7F (used to force the ascii
  88. // parity bit to be stripped).
  89. //
  90. // -------------------------------------------------------------------------------------
  91. // Close/free serial port
  92. - free;
  93. // Close and free the port handling object.  WARNING: If "forkPortListener" has been
  94. // issued, this object should never bee freed!
  95. //
  96. // -------------------------------------------------------------------------------------
  97. // Return actual device name used for specified port number
  98. + (const char *)getPortDevice:(int)portNum;
  99. // This method returns a constant specifying the device name corresponding to the specified
  100. // port number.
  101. //
  102. // -------------------------------------------------------------------------------------
  103. // Return the Baud rate flag constant for the specified baud rate number
  104. + (u_char)baudRateConstant:(int)baudRate;
  105. // BaudRate may be any of the following: 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
  106. // 1800, 2400, 4800, 9600, 19200, or 38400.
  107. //
  108. // -------------------------------------------------------------------------------------
  109. // Write to port
  110. - (int)writeToPort:(char *)buff length:(int)buffLen;
  111. - (int)writeToPort:(char *)buff;
  112. // These methods write data to the serial port.  'writeToPort:' writes a null terminated
  113. // string. and 'writeToPort:length:' writes buffLen characters to the port.
  114. //
  115. // -------------------------------------------------------------------------------------
  116. // Read from port
  117. - (int)read:(char *)buff maxLen:(int)maxLen timeout:(float)timeout;
  118. // This methods ateempt to read maxLen number of bytes from the serial port.  if no
  119. // characters are present after timeout seconds, -1 is returned, otherwise the actual
  120. // number of characters read is returned.
  121. //
  122. // -------------------------------------------------------------------------------------
  123. // Continue reading characters from port
  124. - readLoop:(float)timeout;
  125. // This method will continue to wait for characters to be ready on the serial port.
  126. // When characters are available, they will be read and sent to the data handler object
  127. // via a method call to 'dataRecord:len:fromPort:'.  Then this method will resume waiting
  128. // for characters to read on the serial port.  This method should only be used within
  129. // a backgroup thread (see 'forkPortListener').
  130. //
  131. // -------------------------------------------------------------------------------------
  132. // Wait for available characters on serial port
  133. - (int)readTimeout:(float)timeout;
  134. // This method will wait for characters to become available on the serial port and return
  135. // a value greater than zero if characters are available, 0 if the timeout has elapsed, 
  136. // and a value less than zero if an error has occurred.
  137. //
  138. // -------------------------------------------------------------------------------------
  139. // Start/stop serial port data collector
  140. - forkPortListener;
  141. // This method starts the thread controlled serial port data reader.  'forkPortListener'
  142. // forks a thread and issues method call 'readLoop:0.0'.  Once this method has been
  143. // issued, this object should persist for the remainder of the life of the application.
  144. // (ie. This object should never be 'free'd).  The data read from the port will be
  145. // sent to the data handler method 'dataRecord:len:fromPort:' (see readLoop:).  It is
  146. // important to note that since dataRecord:len:fromPort: will be called from a secondary
  147. // thread the data handler object should not try to perform any drawing in a window view.
  148. //
  149. // -------------------------------------------------------------------------------------
  150. // Return error flags
  151. + (int)openError;
  152. - (int)portError;
  153. // These methods may be used to help with debugging errors which may occur while 
  154. // openning a serial port, or while writing, or reading, a serial port. Possible errors
  155. // are: noERROR, errorOPEN, errorTIOCGETP, errorTIOCSETP, errorBADPORTNUM, errorTIOCEXCL,
  156. // errorTIOCSETD, errorTIOCCBRK, errorTIOCSDTR, errorTIOCLSET, errorTIOCNOTTY,
  157. // or errorTIOCSLTC.
  158. //
  159. // -------------------------------------------------------------------------------------
  160. // Set data handler for "readLoop:"
  161. - setDataHandler:handler;
  162. - (BOOL)dataRecord:(char*)buff len:(int)len fromPort:(int)portNum;
  163. // 'setDataHandler:' specifies an object that will be messaged within 'readLoop:' when
  164. // characters have been read from the serial port.  The method sent to the data handler
  165. // is 'dataRecord:len:formPort:'.  buff will contain the characters read from the port,
  166. // len will specify the number of characters read, and portNum will be the serail port
  167. // from which the characters were read.
  168. //
  169. // -------------------------------------------------------------------------------------
  170. // Set maximum buffer read size
  171. - setMaxReadSize:(int)size;
  172. // This method sets the maximum number of bytes read from the serial port during a 
  173. // single read.  The default is 1024.
  174. //
  175. // -------------------------------------------------------------------------------------
  176.  
  177. @end
  178.